package dao.impl;

import dao.UserDao;
import domain.Book;
import domain.User;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class UserDaoImpl implements UserDao {
    @Override
    public Boolean checkPassword(String username, String password) {
        Connection connection =null;
        Statement statement =null;
        String passwordTrue=null;
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            ResultSet resultSet= statement.executeQuery("SELECT password FROM user WHERE username='"+username+"'");
            if(resultSet.next()){
                passwordTrue=resultSet.getString("password");
            }else {
                passwordTrue="";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
        return password.equals(passwordTrue);
    }

    @Override
    public User getUserInfo(String username) {
        return getUser("SELECT uid,avatar,name,password,username FROM user WHERE username="+username);
    }

    @Override
    public User getUserInfo(int uid) {
        return getUser("SELECT uid,avatar,name,password,username FROM user WHERE uid="+uid);
    }

    @Override
    public List<Book> getUserBook(int uid) {
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;
        List<Book> bookList =new ArrayList<>();
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            resultSet= statement.executeQuery("SELECT bid,cover,NAME,brief,creation_time FROM book WHERE uid="+uid);
            while (resultSet.next()){
                //int bid, int uid, String cover, String name, String brief, Date createionTime
                bookList.add(new Book(
                        resultSet.getInt("bid"),
                        uid,
                        resultSet.getString("cover"),
                        resultSet.getString("name"),
                        resultSet.getString("brief"),
                        resultSet.getDate("creation_time")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
        return bookList;
    }

    @Override
    public void addUser(String avatar, String name, String username, String password, String email) {
        Connection connection =null;
        Statement statement =null;
        Map<String, Date> map = new TreeMap<>();
        try {
            connection = JDBCUtils.getConnection();
            String sql =String.format("INSERT INTO user(avatar,name,username,password,email) VALUES('%s','%s','%s','%s','%s')",avatar,name,username,password,email);
            statement= connection.createStatement();
            statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
    }

    @Override
    public int getLastHistory(int uid) {
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;

        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            resultSet= statement.executeQuery("SELECT last_history FROM user WHERE uid="+uid);
            if (resultSet.next()){
                //int bid, int uid, String cover, String name, String brief, Date createionTime
                return resultSet.getInt("last_history");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
        return 0;
    }

    @Override
    public List<Integer> getCollect(int uid) {
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;
        List<Integer> bidList = new ArrayList<>();
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            resultSet= statement.executeQuery("SELECT bid FROM collect WHERE uid="+uid);
            while (resultSet.next()){
                //int bid, int uid, String cover, String name, String brief, Date createionTime
                bidList.add(resultSet.getInt("bid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
        return bidList;
    }

    @Override
    public void delCollect(int uid, int bid) {
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            statement.execute("DELETE FROM collect WHERE uid="+uid+" AND bid="+bid);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
    }

    @Override
    public void addCollect(int uid, int bid) {
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            statement.execute("INSERT INTO collect(uid,bid) VALUES("+uid+","+bid+");");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
    }


    private User getUser(String sql){
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet;
        User user = new User();
        try {
            connection = JDBCUtils.getConnection();
            statement= connection.createStatement();
            resultSet= statement.executeQuery(sql);
            if (resultSet.next()){
                user.setAvatar(resultSet.getString("avatar"));
                user.setUsername(resultSet.getString("username"));
                user.setName(resultSet.getString("name"));
                user.setUid(Integer.parseInt(resultSet.getString("uid")));
                user.setPassword(resultSet.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(statement,connection);
        }
        return user;
    }
}
